package com.santu.edu.supporter.biz.pay.controller;

import com.santu.edu.supporter.biz.pay.pojo.constants.WxPayConstants;
import com.santu.edu.supporter.biz.pay.service.WxOrderService;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 微信 - 用户订单管理
 */
@RestController
@RequestMapping("/wx/order")
@Validated
@RequiredArgsConstructor
public class WxOrderController {

    private final WxOrderService wxOrderService;

    private final WxPayConstants wxPayConfig;

    @Resource
    private Verifier verifier;


    /**
     * 订单支付后回调
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("wxOrderCallBack")
    public Boolean wxOrderCallBack(HttpServletRequest request, HttpServletResponse response){
        return wxOrderService.wxOrderCallBack(request,response);
    }

/*    *//**
     * 订单退款
     * @param orderId
     * @param orderType
     * @return
     *//*
    @GetMapping("orderRefund")
    public Boolean orderRefund(@NotBlank(message = "orderId不能为空") String orderId,
                               @NotNull(message = "orderType不能为空") Integer orderType){
        return wxOrderService.orderRefund(orderId,orderType);
    }*/

/*    *//**
     * 微信退款回调接口
     * @param request
     * @param response
     * @return
     *//*
    @PostMapping("refundWechatCallback")
    public String refundWechatCallback(HttpServletRequest request, HttpServletResponse response) {
        return WxPayRefundCallbackUtil.wxPayRefundCallback(request, response, verifier, wxPayConfig, new WechatRefundCallback() {
            @Override
            public void success(WxChatCallbackRefundDto refundData) {
                // TODO 退款成功的业务逻辑，例如更改订单状态为退款成功等
                Order one = wxOrderService.getOne(new QueryWrapper<Order>()
                        .eq("order_number", refundData.getOrderId()));
                one.setOrderStatus(4);
                wxOrderService.updateById(one);
            }

            @Override
            public void fail(WxChatCallbackRefundDto refundData) {
                // TODO 特殊情况下退款失败业务处理，例如银行卡冻结需要人工退款，此时可以邮件或短信提醒管理员，并携带退款单号等关键信息
                System.out.println("退款失败");
            }
        });
    }*/

}
